var db = wx.cloud.database()
Page({

  data: {
    goodsList: [],
    timer:0,
    searchVal:''
  },
  searchInput(e) {
    // 获取输入的内容
    console.log(e);
    const { value } = e.detail
    this.setData({
      searchVal:value
    })
    // 判断是否为空字符
    if (value.trim().length === 0) {
      return
    }
    // 防抖  避免多次请求
    clearTimeout(this.timer)
    this.timer= setTimeout(() => {
      this.qsearch(value)
    }, 1000)

  },
// 进行搜索
qsearch(value) {
  let that= this
  let {goodsList} = this.data
  wx.showLoading({
    title: '加载中',
    mask:true
  })
   db.collection('goodsDetail').where({
    goods_name: db.RegExp({
      regexp: this.data.searchVal,//做为关键字进行匹配
      options: 'i',//不区分大小写
    })
   })
   .get() 
   .then(res=>{
     console.log(res);
     that.setData({
      goodsList:res.data
     })
   })
   wx.hideLoading()
},

  handleCancel() {
      this.setData({
        searchVal:'',
        goodsList:[]
      })
  }


})